package org.yn.exception;

import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.Objects;

/**
 * 断言工具类
 * 当断言某个对象为空时不为空就抛出错误
 */
public class AssertUtil {



    // 断言字符串为空
    public static void isBlank(String str,String message){
        // 当字符串不为空抛出异常
        if(StringUtils.isNotBlank(str)){
            throw new BusinessException(message);
        }
    }

    // 断言字符串不为空
    public static void isNotBlank(String str,String message){
        // 字符串为空,抛出异常
        if(StringUtils.isBlank(str)){
            throw new BusinessException(message);
        }
    }

    // 断言对象为空
    public static void isNull(Object obj,String message){
        if(Objects.nonNull(obj)){
            throw new BusinessException(message);
        }
    }

    // 断言对象不为空
    public static void isNotNull(Object obj,String message){
        if(Objects.isNull(obj)){
            throw new BusinessException(message);
        }
    }

    // 断言集合为空
    public static void isNull(Collection coll,String message){
        // 当集合不为空且长度>0的时候,抛出异常
        if(coll != null && coll.size() > 0){
            throw new BusinessException(message);
        }
    }

    // 断言集合不为空
    public static void isNotNull(Collection coll,String message){
        // 当集合为空 或者 集合的长度=0,抛出异常
        if(coll == null || coll.size() == 0){
            throw new BusinessException(message);
        }
    }


    // 断言两个字符串相等
    public static void isEq(String str1,String str2,String message){
        if(!str1.equals(str2)){
            throw new BusinessException(message);
        }
    }



    // 断言字符串为空
    public static void isBlank(String str,ResponseCode responseCode){
        // 那么当字符串不为空,就要抛出异常
        if(StringUtils.isNotBlank(str)){
            throw new BusinessException(responseCode);
        }
    }

    // 断言字符串不为空
    public static void isNotBlank(String str,ResponseCode responseCode){
        // 那么当字符串为空,就要抛出异常
        if(StringUtils.isBlank(str)){
            throw new BusinessException(responseCode);
        }
    }

    // 断言对象为空
    public static void isNull(Object obj,ResponseCode responseCode){
        if(Objects.nonNull(obj)){
            throw new BusinessException(responseCode);
        }
    }

    // 断言对象不为空
    public static void isNotNull(Object obj,ResponseCode responseCode){
        if(Objects.isNull(obj)){
            throw new BusinessException(responseCode);
        }
    }

    // 断言集合为空
    public static void isNull(Collection coll,ResponseCode responseCode){

        if(coll != null && coll.size() > 0){// 当集合不为空且长度>0的时候,抛出异常
            throw new BusinessException(responseCode);
        }
    }

    // 断言集合不为空
    public static void isNotNull(Collection coll,ResponseCode responseCode){
        // 当集合为空 或者 集合的长度=0,抛出异常
        if(coll == null || coll.size() == 0){
            throw new BusinessException(responseCode);
        }
    }

    // 断言两个字符串相等
    public static void isEq(String str1,String str2,ResponseCode responseCode){
        if(!str1.equals(str2)){
            throw new BusinessException(responseCode);
        }
    }

    /**
     * 判断两个字符串是否相等，忽略大小写
     * @param str1
     * @param str2
     * @param responseCode
     */
    public static void isEqIgnoreCase(String str1,String str2,ResponseCode responseCode){
        if(!str1.equalsIgnoreCase(str2)){
            throw new BusinessException(responseCode);
        }
    }


    /**
     * 断言为0，  不为0就报错
     * @param value
     * @param responseCode
     */
    public static  void isZer0(int value,ResponseCode responseCode){
        if (value>0){
            throw new BusinessException(responseCode);
        }
    }
    
}
